package com.liury.bbs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liury.bbs.entity.Kind;
import com.liury.bbs.entity.Reply;
import com.liury.bbs.entity.Title;
import com.liury.bbs.entity.User;
import com.liury.bbs.mapper.KindMapper;
import com.liury.bbs.mapper.ReplyMapper;
import com.liury.bbs.mapper.TitleMapper;
import com.liury.bbs.mapper.UserMapper;
import com.liury.bbs.service.IndexService;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.time.LocalDateTime;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@Controller
public class IndexController {
    @Autowired
    KindMapper kindMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TitleMapper titleMapper;
    @Autowired
    ReplyMapper replyMapper;
    @Autowired
    IndexService indexService;
    @RequestMapping("index")
    public String index(Model model, HttpSession session){
        List<Kind> k= indexService.findAll();
        model.addAttribute("k",k);

        int usercount=userMapper.countuser();
        int titlecount=titleMapper.counttitle();
        int replycount=replyMapper.countreply();
        session.getServletContext().setAttribute("usercount",usercount);
        session.getServletContext().setAttribute("titlecount",titlecount);
        session.getServletContext().setAttribute("replycount",replycount);
        return "index";
    }

    //转注册
    @RequestMapping("toRegister")
    public  String toRegister(){
        return "register";
    }



//注册检查非法用户名
    @RequestMapping("check1")
    @ResponseBody
    public String check(String username){
        User user=userMapper.checkName(username);
        if(user!=null)
            return "0";
        else return "1";
    }

    //注册
    @RequestMapping("insert")
    public String insert(User user){
        String data=LocalDateTime.now().toString();
        user.setRegedit_time(data);
        userMapper.insert(user);
        return "redirect:index";
    }

    @RequestMapping("login")
    public String login(String name,String password,HttpSession session){
        User loginuser=userMapper.login(name,password);
        if(loginuser!=null){
            session.getServletContext().setAttribute("loginuser",loginuser);
            return "redirect:index";
        }else {

            session.getServletContext().setAttribute("msg","用户名不存在或密码错误");
            return "redirect:index";
        }

    }


    //退出登录
    @RequestMapping("goout")
    public String goout(HttpSession session){
        session.getServletContext().removeAttribute("loginuser");
        return "redirect:index";
    }


    //查看帖子
    @RequestMapping("getTitlesBykid")
    public String getTitlesBykid(@RequestParam(defaultValue = "1") Integer pageNo, Integer kindid, HttpSession session, Model model){
        PageHelper.startPage(pageNo,5);
        List<Title> titlelist=titleMapper.getTitlesByKid(kindid);
        PageInfo<Title> titlePageInfo =new PageInfo<>(titlelist);
        model.addAttribute("titlePageInfo",titlePageInfo);
        Kind kind=kindMapper.get(kindid);
        session.setAttribute("kind",kind);
        for(Title t:titlelist){
            int r=replyMapper.countreplybytitle(t.getId());
            t.setReplyCount(r);
        }
        return "titlelist";

    }

    //go发表新帖界面
    @RequestMapping("tonewtitle")
    public String tonewtitle(){
        return "newtitle";
    }

    //发表新帖
    @RequestMapping("insertTitle")
    public String insertTitle(Title t){
        titleMapper.insert(t);
        return "redirect:getTitlesBykid?kindid="+t.getKind_id();
    }

    //查看回复
    @RequestMapping("getRepliesByTid")
    public String getRepliesByTid(Integer titleid,@RequestParam(defaultValue = "1") Integer pageNo,Model model){
        PageHelper.startPage(pageNo,2);
        List<Reply> replyList=replyMapper.getRepliesByTid(titleid);
        PageInfo<Reply> replyPageInfo=new PageInfo<>(replyList);
        model.addAttribute("replyPageInfo",replyPageInfo);
        Title title=titleMapper.getTitle(titleid);
        model.addAttribute("title",title);
        return "replylist";

    }

    //回帖
    @RequestMapping("insertReply")
    public String insertReply(Reply reply){
        replyMapper.insert(reply);
        return "redirect:/getRepliesByTid?titleid="+reply.getTitle_id();
    }

    @RequestMapping("toPreIndex")
    public String toPreIndex(){
        return "redirect:index";
    }
}
